package com.lg.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.ConcurrentAccessException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lg.annotation.LoggerMessage;
import com.lg.mapper.JobExecutionLogMapper;
import com.lg.pojo.JobExecutionLog;
import com.lg.pojo.JobExecutionLogExample;
import com.lg.pojo.User;
import com.lg.service.UserService;

@Controller
public class MyController {
	
	@Autowired
	private UserService userService;

	@RequestMapping("/login")
	@LoggerMessage(url="/login",method=HttpMethod.GET,userName="fisrt")
	public String login() {
		userService.login();
		return "login";
	}
	@RequestMapping("/regUser")
	public String regUser() {
		return "reg";
	}
	@RequestMapping(value="/subRegUser",method=RequestMethod.POST)
	public String subRegUser(User user,Map map) {
		try {
			
			userService.addUser(user);
			map.put("info", "注册成功");
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return "login";
	}
	@RequestMapping(value="/sublogin",method=RequestMethod.POST)
	@LoggerMessage(url="/sublogin",method=HttpMethod.POST,userName="fisrt")
	public String login(User user,Map map) {
		Subject subject = SecurityUtils.getSubject();
		AuthenticationToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword(),
				user.getRememberMe());
		try {
			subject.login(token);
			boolean authenticated = subject.isAuthenticated();
			if (authenticated) {
				return "home";
			}
		} catch (ConcurrentAccessException e) {
			// TODO Auto-generated catch block
			map.put("info", "当前用户已经登录");
           
		} catch (LockedAccountException e) {
			// TODO Auto-generated catch block
			map.put("info", "该账户已经被锁定");
           
		}  catch (ExcessiveAttemptsException e) {
			// TODO Auto-generated catch block
			map.put("info", "该账户已经超过尝试时间");
           
		} catch (UnknownAccountException e) {
			// TODO Auto-generated catch block
			map.put("info", "该账户不存在");
           
		} catch (ExpiredCredentialsException e) {
			// TODO Auto-generated catch block
			map.put("info", "密码过期");
           
		}catch (IncorrectCredentialsException e) {
			// TODO Auto-generated catch block
			map.put("info", "密码错误");
           
		}catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return "login";
	}

	@RequestMapping(value = "/check", method = RequestMethod.GET)
	@ResponseBody
	public String checkLogin() {
		Subject subject = SecurityUtils.getSubject();
		Object principal = subject.getPrincipal();
		subject.hasAllRoles(Arrays.asList("admin"));
		return "loginSuccess";
	}
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	@ResponseBody
	public String logout() {
		User findFullUserById = userService.findFullUserById(1);
		User findFullUserById2 = userService.findFullUserById2(1);
		System.out.println();
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "logoutSuccess";
	}
	public static void main(String[] args) {
		Md5Hash md5Hash = new Md5Hash("123456", "1", 1);
		System.out.println(md5Hash);
	}
}
